package org.example.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.benmanes.caffeine.cache.Cache;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.sys.entity.SysAccount;
import org.example.sys.mapper.SysAccountMapper;
import org.example.utlis.ResponseUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author joey
 */
@RestController
@Slf4j
public class IndexController {
    @Resource
    Cache<String, Object> caffeineCacheAuth;
    @Resource
    SysAccountMapper sysAccountMapper;


    @ApiOperation(value = "检验token")
    @PostMapping("/pwd/checkToken")
    public ResponseUtil checkToken(HttpServletRequest request){
        try {
            String token = request.getParameter("token");
            if(StrUtil.isEmpty(token)){
                return ResponseUtil.noAuthority3();
            }
            Object obj = caffeineCacheAuth.getIfPresent(token);
            if(obj==null){
                return ResponseUtil.noAuthority3();
            }
            return ResponseUtil.success();
        }catch (Exception e){
            log.error("检验token出错",e);
            return ResponseUtil.error();
        }
    }


    @ApiOperation(value = "密码模式-注册")
    @PostMapping("/pwd/register")
    public ResponseUtil pwdRegister(HttpServletRequest request){
        try {
            String accountName = request.getParameter("accountName");
            String accountPwd = request.getParameter("accountPwd");
            String mobileNumber = request.getParameter("mobileNumber");
            if(StrUtil.isEmpty(accountName)||StrUtil.isEmpty(accountPwd)){
                return ResponseUtil.error("帐号或密码不能为空");
            }
            QueryWrapper qw = new QueryWrapper();
            qw.eq("accountName",accountName.trim());
            SysAccount sysAccount = sysAccountMapper.selectOne(qw);
            if(sysAccount!=null){
                return ResponseUtil.error("帐号已存在");
            }
            SysAccount a = new SysAccount();
            a.setAccountName(accountName);
            a.setAccountPwd(accountPwd);
            a.setCreateTime(LocalDateTime.now());
            a.setMobileNumber(mobileNumber);
            sysAccountMapper.insert(a);
            return ResponseUtil.success();
        }catch (Exception e){
            log.error("密码模式注册出错",e);
            return ResponseUtil.error(e.getMessage());
        }
    }
    @ApiOperation(value = "密码模式-登陆")
    @PostMapping("/pwd/login")
    public ResponseUtil pwdLogin(HttpServletRequest request){
        try {
            String accountName = request.getParameter("accountName");
            String accountPwd = request.getParameter("accountPwd");
            if(StrUtil.isEmpty(accountName)||StrUtil.isEmpty(accountPwd)){
                return ResponseUtil.error("帐号或密码不能为空");
            }
            QueryWrapper qw = new QueryWrapper();
            qw.eq("accountName",accountName.trim());
            SysAccount sysAccount = sysAccountMapper.selectOne(qw);
            if(sysAccount==null){
                return ResponseUtil.error("帐号名错误");
            }
            if(!sysAccount.getAccountPwd().equals(accountPwd.trim())){
                return ResponseUtil.error("密码错误");
            }
            String token = IdUtil.fastSimpleUUID();
            caffeineCacheAuth.put(token, JSONObject.toJSONString(sysAccount));
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("token",token);
            resultMap.put("accountId",sysAccount.getAccountId());
            return ResponseUtil.success(resultMap);
        }catch (Exception e){
            log.error("密码模式-登陆出错",e);
            return ResponseUtil.error(e.getMessage());
        }
    }

}
